'''
Created on Oct 11, 2012

@author: daeheehan

Modified on Oct 31, 2012
@author: gyucheol
'''

from pprint import pprint
from github3 import login, GitHub
from getpass import getpass, getuser

import datetime
import dateutil.parser
import time
from pprint import pprint
import sys
from types import *

# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_ID_1 = "booknaratest"
GITHUB_USER_ID_2 = "jangho87"
GITHUB_USER_ID_3 = 'skswhwo'
GITHUB_USER_ID_4 = 'luispigu'

GITHUB_USER_PW = "qwer1234"
GITHUB_USER_PW_1 = "wasd1234"

if __name__ == '__main__':
    pass

gh = login(GITHUB_USER_ID_1, password=GITHUB_USER_PW)
print gh.ratelimit_remaining, 'request remaining'

# Github Rails Repository information
GITHUB_USER_NAME = "rails"
GITHUB_REPOSITORY_NAME = "rails"

# Rails changer commenter information file
GRAPH_FILE = 'rails_changer_commenters7000_8348.txt'
#GRAPH_FILE = 'rails_changer_commenters4.txt'
#GRAPH_FILE = 'rails_changer_commenters800txt'
#GRAPH_FILE = 'rails_changer_commenters1600.txt'
#GRAPH_FILE = 'rails_changer_commenters2400.txt'
#GRAPH_FILE = 'rails_changer_commenters3200.txt'

NUM_TRAINING_ISSUE_START = 7000
NUM_TRAINING_ISSUE_END = 8349

issue_commenters = []
changer_commenters = {}

try:
    repos = gh.repository(GITHUB_USER_NAME, GITHUB_REPOSITORY_NAME)
    
    # Training-Set(Pull Request number: 4 ~ 4249)
#     Except for No.1336, 1338, 2176 (HTTP 404 Error)
#    for i in range(4, 800):
#    for i in range(800, 1600):
#    for i in range(1600, 2400):
#    for i in range(2400, 3200):
#    for i in range(3200, 4249):
    for i in range(NUM_TRAINING_ISSUE_START, NUM_TRAINING_ISSUE_END):
        try:
            print i
            pull = repos.pull_request(i)
            issue = repos.issue(i)
            
#            if (str(pull.is_merged()) == 'False'):
#                continue
            # Open/Close, Merged or Not, Pull Requester, Committer(Merged by), Commenters 
            list_edgeNode = []
            list_edgeNode.append(', ' + str(pull.state))
            list_edgeNode.append(', ' + str(pull.is_merged()))
            list_edgeNode.append(', ' + pull.user.login)
            if (type(pull.merged_by) == NoneType):
                list_edgeNode.append(', None')
            else:
                list_edgeNode.append(', ' + pull.merged_by.login)
#            print pull.user.login, pull.html_url, pull.is_merged(), pull.created_at, pull.state, pull.merged_by.login

#            pprint(vars(pull))
#            print issue.comments
             
            for comment in issue.iter_comments():
#                print comment.user.login
#                print comment
#                pprint(vars(comment))
                list_edgeNode.append(', ' + comment.user.login)

                if(gh.ratelimit_remaining < 100):
                    time.sleep(60 * 60)

            commenters_set = ''.join(list_edgeNode)
            print commenters_set
            changer_commenters[i] = commenters_set
    
        except Exception as e:
            print 'The inner Exception', e.args[0]
            
#        break
#    print 'Printing dictionary: Graph'
#    f_graph = open(GRAPH_FILE, 'w')
#    f_graph.write('Id, Open/Close, Merged or Not, Pull Requester, Committer(Merged by), Commenters \n')
#    for key, value in sorted(changer_commenters.items()):
#        sys.stdout.write(str(key) + str(value) + '\n')
#        f_graph.write(str(key) + str(value) + '\n')
#
#    f_graph.close()

except Exception as e: 
    print 'The final Exception', e.args[0]

print gh.ratelimit_remaining, 'request remaining'

print
print 'Finally the end'
